package com.faxsun.web.notification.impl;

import java.text.MessageFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.faxsun.http.HttpInvoker;
import com.faxsun.web.notification.FaxsunInfoNotfier;
import com.faxsun.web.notification.exception.ParameterEmptyException;
import com.faxsun.web.processor.FSOnePageCheckoutProcessor;

/**
 * 电商调用资讯的api，发送合并帐户请求
 * 
 * @author songdragon
 *
 */
@Service("fsInfoNotifier")
public class FaxsunInfoNotifierImpl implements FaxsunInfoNotfier {
	
	protected static final Log LOG = LogFactory.getLog(FaxsunInfoNotifierImpl.class);

	@Value("${faxsun.info.base}")
	private String FAXSUNINFO_BASE;

	@Value("${faxsun.info.bindAccount}")
	private String FAXSUNINFO_BINDACCOUNT;
	
	@Value("${faxsun.info.unbindAccount}")
	private String FAXSUNINFO_UNBINDACCOUNT;

	@Value("${faxsun.info.mergeAccount}")
	private String FAXSUNINFO_MERGEACCOUNT;

	@Override
	public boolean notifyBindAccount(long customerId, String openId, String channelId,
			String unionId) throws ParameterEmptyException {
		if (StringUtils.isEmpty(openId) || StringUtils.isEmpty(channelId)) {
			throw new ParameterEmptyException();
		}
		try {
			String url = MessageFormat.format(FAXSUNINFO_BASE + FAXSUNINFO_BINDACCOUNT, openId, String.valueOf(customerId), channelId,unionId);
			String response = HttpInvoker.get(url);
			JSONObject responseObject = (JSONObject) JSON.parse(response);
			if (responseObject.containsKey("data") && responseObject.containsKey("code")) {
				boolean result = responseObject.getBooleanValue("data");
				return result;
			}
		} catch (Exception e) {
			LOG.error("",e);
		}
		return false;
	}

	@Override
	public boolean notifyMergeAccount(final long fromId, final long toId) throws ParameterEmptyException{
		if (fromId<=0 ||toId<=0) {
			throw new ParameterEmptyException();
		}
		try {
			String url = MessageFormat.format(FAXSUNINFO_BASE + FAXSUNINFO_MERGEACCOUNT, fromId,toId);
			String response = HttpInvoker.get(url);
			JSONObject responseObject = (JSONObject) JSON.parse(response);
			if (responseObject.containsKey("data") && responseObject.containsKey("code")) {
				boolean result = responseObject.getBooleanValue("data");
				return result;
			}
		} catch (Exception e) {
			LOG.error("",e);
		}
		return false;
	}

	@Override
	public boolean notifyMergeAccount(long openIdFrom, long customerIdTo, String channelId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean notifyUnbindAccount(long customerId, String channelId) {
		//TODO:将解除绑定结果写入数据库，由后台线程尝试发起请求
		try {
			String url = MessageFormat.format(FAXSUNINFO_BASE + FAXSUNINFO_UNBINDACCOUNT,  customerId, channelId);
			String response = HttpInvoker.get(url);
			JSONObject responseObject = (JSONObject) JSON.parse(response);
			if (responseObject.containsKey("data") && responseObject.containsKey("code")) {
				boolean result = responseObject.getBooleanValue("data");
				return result;
			}
		} catch (Exception e) {
			LOG.error("",e);
		}
		return false;
	}

}
